package com.emmmya.harinCheck.engine;

import com.emmmya.harinCheck.exception.HarinCheckException;
import com.emmmya.harinCheck.result.CheckResult;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 校验引擎抽象类，用户可以自定义校验方法
 * @author Harin
 * @version 1.0
 * @date 2022-04-29 19:30:53
 */
public interface HarinCheckEngine {

    Class<?>[] classes = new Class[]{int.class,long.class,float.class,double.class,
            char.class,boolean.class,byte.class,short.class,String.class};

    CheckResult check(Object obj,String[] fliedNames) throws HarinCheckException, IllegalAccessException;


    CheckResult check(Object obj,String[] fliedNames,Boolean deep) throws HarinCheckException, IllegalAccessException;

    /**
     * 获取所有的字段
     */
    default Field[] getAllFields(Object obj) {
        if(obj == null ) return null;
        return getAllFields(obj.getClass());

    }

    /**
     * 获取所有的字段
     */
    default Field[] getAllFields(Class<?> clazz) {
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        return fieldList.toArray(fields);
    }


}
